/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.biz.service.impl;

import com.cib.fintech.entrustedinvestment.biz.domain.BookValue;
import com.cib.fintech.entrustedinvestment.biz.repository.BookValueRepository;
import com.cib.fintech.entrustedinvestment.biz.service.BookValueService;
import com.cib.fintech.entrustedinvestment.biz.service.dto.BookValueDto;
import com.cib.fintech.entrustedinvestment.biz.service.dto.BookValueQueryCriteria;
import com.cib.fintech.entrustedinvestment.biz.service.mapstruct.BookValueMapper;
import com.cib.fintech.entrustedinvestment.utils.ValidationUtil;
import com.cib.fintech.entrustedinvestment.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.cib.fintech.entrustedinvestment.utils.PageUtil;
import com.cib.fintech.entrustedinvestment.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-09-17
**/
@Service
@RequiredArgsConstructor
public class BookValueServiceImpl implements BookValueService {

    @Autowired
    private  BookValueRepository tBokValRepository;

    @Autowired
    private  BookValueMapper tBokValMapper;

    @Override
    public Map<String,Object> queryAll(BookValueQueryCriteria criteria, Pageable pageable){
        Page<BookValue> page = tBokValRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tBokValMapper::toDto));
    }

    @Override
    public List<BookValueDto> queryAll(BookValueQueryCriteria criteria){
        return tBokValMapper.toDto(tBokValRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BookValueDto findById(Integer seqNo) {
        BookValue tBokVal = tBokValRepository.findById(seqNo).orElseGet(BookValue::new);
        ValidationUtil.isNull(tBokVal.getSeqNo(),"BookValue","seqNo",seqNo);
        return tBokValMapper.toDto(tBokVal);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BookValueDto create(BookValue resources) {
        return tBokValMapper.toDto(tBokValRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BookValue resources) {
        BookValue tBokVal = tBokValRepository.findById(resources.getSeqNo()).orElseGet(BookValue::new);
        ValidationUtil.isNull( tBokVal.getSeqNo(),"BookValue","id",resources.getSeqNo());
        tBokVal.copy(resources);
        tBokValRepository.save(tBokVal);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer seqNo : ids) {
            tBokValRepository.deleteById(seqNo);
        }
    }

    @Override
    public void download(List<BookValueDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BookValueDto tBokVal : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("产品代码", tBokVal.getBooksetId());
            map.put("产品名称", tBokVal.getBooksetName());
            map.put("收益类型", tBokVal.getProfitType());
            map.put("估值日期", tBokVal.getValDate());
            map.put("明细区分", tBokVal.getDetailDist());
            map.put("科目号", tBokVal.getSubjetNo());
            map.put("科目名称", tBokVal.getSubjectName());
            map.put("上级科目号", tBokVal.getFsubjectId());
            map.put("数量", tBokVal.getNumAmt());
            map.put("单位成本", tBokVal.getUnitCost());
            map.put(" cost",  tBokVal.getCost());
            map.put("本币成本占比", tBokVal.getCostPercent());
            map.put("原币市值", tBokVal.getMarketValue());
            map.put("本币市值占比", tBokVal.getValuePercent());
            map.put("本币市值占比", tBokVal.getValueIncrement());
            map.put(" balFlag",  tBokVal.getBalFlag());
            map.put("影子价格估值", tBokVal.getShadowPriceValue());
            map.put("创建人", tBokVal.getCreateUser());
            map.put("创建部门", tBokVal.getCreateDept());
            map.put("创建时间", tBokVal.getCreateTime());
            map.put("更新人", tBokVal.getUpdateUser());
            map.put("更新时间", tBokVal.getUpdateTime());
            map.put("行情日期", tBokVal.getMarketValDate());
            map.put("分层代码", tBokVal.getLayeringId());
            map.put("行情", tBokVal.getClosePrice());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}